//
//  CustomChooseTypeView.swift
//  胖子弹框
//
//  Created by 赵宇航 on 2017/9/19.
//  Copyright © 2017年 赵宇航. All rights reserved.
//

import UIKit

typealias returnChooseType = (String) ->()

class CustomChooseTypeView: UIView,UIPickerViewDelegate,UIPickerViewDataSource {
    
    var pickerView: UIPickerView!
    var dataArr: NSArray!
    var chooseType: String!
    var titleLabel: UILabel!
    var returnType: returnChooseType?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpSubViews()
    }
    
    func creatWithDataAndTitle(data: NSArray,title: String) {
        dataArr = data
        chooseType = data[0] as! String
        titleLabel.text = title
        pickerView.reloadAllComponents()
    }
    
    convenience init() {
        self.init(frame: CGRect.zero)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpSubViews() {
        self.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        let view = UIView.init(frame: CGRect(x:0,y:self.frame.size.height - 260,width:self.frame.size.width,height:260))
        view.backgroundColor = UIColor.white
        
        let topView = UIView.init(frame: CGRect(x:0,y:0,width:self.frame.size.width,height:40))
        topView.backgroundColor = UIColor.init(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
        
        let cancelBtn = UIButton.init(type: UIButtonType.custom)
        cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        cancelBtn.setTitleColor(UIColor.lightGray, for: UIControlState.normal)
        cancelBtn.frame = CGRect(x: 20,y: 0,width: 40,height: 40)
        cancelBtn.setTitle("取消", for: UIControlState.normal)
        cancelBtn.addTarget(self, action: #selector(clickCancel), for: .touchUpInside)
        topView.addSubview(cancelBtn)
        
        let connectBtn = UIButton.init(type: UIButtonType.custom)
        connectBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        connectBtn.setTitleColor(UIColor.red, for: UIControlState.normal)
        connectBtn.frame = CGRect(x: self.frame.size.width - 60,y: 0,width: 40,height: 40)
        connectBtn.setTitle("确定", for: UIControlState.normal)
        connectBtn.addTarget(self, action: #selector(clickConnect), for: .touchUpInside)
        topView.addSubview(connectBtn)
        
        titleLabel = UILabel.init(frame: CGRect(x: 60,y: 10,width:self.frame.size.width - 120,height: 20))
        titleLabel.textAlignment = NSTextAlignment.center
        topView.addSubview(titleLabel)
        view.addSubview(topView)
        
        pickerView = UIPickerView.init(frame: CGRect(x: 0,y: 40,width: self.frame.size.width,height: 230))
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.showsSelectionIndicator = true
        view.addSubview(pickerView)
        
        self.addSubview(view)
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataArr.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return dataArr[row] as? String
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        chooseType = dataArr[row] as! String
    }
    
    func clickCancel() {
        self.removeFromSuperview()
    }
    
    func clickConnect() {
        returnType!(chooseType)
        self.removeFromSuperview()
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
